import dayjs from 'dayjs';
import type { CaptchaState, EmailState, SmsState } from "@/store/sms";
import { useCaptchaStore } from "@/store/sms";

const templateCode = (type: string) => {
  const templateType = {
    LOGIN: 'OP_LOGIN',
    REGISTER: 'OP_REG'
  }
  return templateType[type as keyof typeof templateType];
}

// 邮箱消息类型
const emailMsgType = (type: string) => {
  const templateType = {
    LOGIN: '10001',
    REGISTER: '10000',
    RESETPWD: '10002',
  }
  return templateType[type as keyof typeof templateType];
}

/**
 * 发送短信验证码  60秒
 * @param event
 * @param mobile
 *  @param type 用户已登录 类型 userIsLogin 否则为空
 */
export async function getSmsCode(event: string, mobile = '') {
  const commonStore = useCaptchaStore();
  const lastSendTimer = commonStore.sms[templateCode(event) as keyof SmsState];
  const duration = dayjs().unix() - lastSendTimer;
  const canSend = duration >= 60;
  console.log(canSend)
  if (!canSend) {
    return ElMessage({
      message: "请稍后再试~",
      type: "warning",
    });
  }
  if (!/^[1][3-9][0-9]{9}$/.test(mobile)) {
    return ElMessage({
      message: "手机号码格式不正确~",
      type: "warning",
    });
  }
  const result = await useApi().app.smsSend({
    templateCode: templateCode(event),
    phoneNo: mobile,
  });
  console.log(result);
  if (result.code === 0) {
    // 设置验证码倒计时
    commonStore.$patch((state: CaptchaState) => {
      state.sms[templateCode(event) as keyof SmsState] = dayjs().unix();
    });
  }
}

/**
 * 获取短信验证码倒计时 -- 60秒
 * @param event
 */
export function getSmsTimer(event: string) {
  const commonStore = useCaptchaStore();
  const lastSendTimer = commonStore.sms[templateCode(event) as keyof SmsState];
  if (typeof lastSendTimer === 'undefined') {
    return ElMessage({
      message: "短信发送事件错误~",
      type: "error",
    });
  }
  const duration = ref(dayjs().unix() - lastSendTimer - 60);
  const canSend = duration.value >= 0;
  if (canSend) {
    return 0;
  }
  // 返回短信验证码倒计时
  if (!canSend) {
    setTimeout(() => {
      duration.value++;
    }, 1000);
    return -duration.value;
  }
}


/**
 * 发送邮箱验证码  60秒
 * @param event
 * @param mobile
 *  @param type 用户已登录 类型 userIsLogin 否则为空
 */
export async function getEmailCode(event: string, email = '') {
  const commonStore = useCaptchaStore();
  const lastSendTimer = commonStore.email[event as keyof EmailState];
  const duration = dayjs().unix() - lastSendTimer;
  const canSend = duration >= 60;
  if (!canSend) {
    return ElMessage({
      message: "请稍后再试~",
      type: "warning",
    });
  }
  const result = await useApi().app.emailSend({
    emailMsgType: emailMsgType(event),
    email: email,
  });
  if (result.code === 0) {
    // 设置验证码倒计时
    commonStore.$patch((state: CaptchaState) => {
      state.email[event as keyof EmailState] = dayjs().unix();
    });
  }
}

/**
 * 获取短信验证码倒计时 -- 60秒
 * @param event
 */
export function getEmailTimer(event: string) {
  const commonStore = useCaptchaStore();
  const lastSendTimer = commonStore.email[event as keyof EmailState];
  if (typeof lastSendTimer === 'undefined') {
    return ElMessage({
      message: "验证码发送事件错误~",
      type: "error",
    });
  }
  const duration = ref(dayjs().unix() - lastSendTimer - 60);
  const canSend = duration.value >= 0;
  console.log(canSend, 'canSend')
  if (canSend) {
    return 0;
  }
  // 返回短信验证码倒计时
  if (!canSend) {
    setTimeout(() => {
      duration.value++;
      console.log(1)
    }, 1000);
    return -duration.value;
  }
}